package com.zhanlv.composer

import android.util.Log
import org.json.JSONObject

object SocketGateway {

    /**
     * @param
     * params: json string
     */
    fun run(payloadJsonStr: String, callback: SocketGatewayInterface){
        try {
            val payloadJsonObj = JSONObject(payloadJsonStr)
            var module = payloadJsonObj.optString("module")
            var action = payloadJsonObj.optString("action")
            var params = payloadJsonObj.optString("params")

            var paramsJsonObject = JSONObject(params)

            var name = paramsJsonObject.optString("name")
            var age = paramsJsonObject.optInt("age")
            var index = paramsJsonObject.optInt("index")

            callback.onResult("hello $name")
            Thread.sleep(1000)

            callback.onResult("your age is $age")
            Thread.sleep(1000)

            callback.onResult("index is $index")
        }catch (e: Exception){
            Log.e("xiaolv", "json parse error")
        }
    }

    interface SocketGatewayInterface{
        fun onResult(res: String)
    }
}